package pro.spring.controller.frontController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import pro.spring.model.Information;
import pro.spring.model.Messages;
import pro.spring.vo.JsonVo;
import pro.spring.vo.PaginationVo;

@Controller("ArticleController")
@RequestMapping("/article")
public class ArticleController extends BaseController{
	private String local = "page/front/come/";
	
	@RequestMapping("/info.html")
	public String oneInfo(Integer inId,ModelMap map,HttpServletRequest req){
		Information information=informationService.selectByPrimaryKey(inId);
		Integer maxId=informationService.selectMaxIdByType(100);
		Integer minId=informationService.selectMinIdByType(100);
		HttpSession session=req.getSession();
		Integer usersUId = (Integer)session.getAttribute("uId");
		PaginationVo<Messages> allMessages=messagesService.getPaginationInfoByInid(inId,1,5);
		map.put("nowpage",1);
		int allpage=messagesService.messagePageNum(inId);
		map.put("allpage", allpage);
		map.put("messages",allMessages);
		map.put("one",information);
		map.put("maxId",maxId);
		map.put("minId", minId);
		map.put("uId", usersUId);
		return local+"wenzhang";
	}
	@RequestMapping("/previous.html")
	public String previousOneInfo(Integer inId,ModelMap map,HttpServletRequest req){
		Information information=informationService.selectByPrimaryKey(inId+1);
		Integer maxId=informationService.selectMaxIdByType(100);
		Integer minId=informationService.selectMinIdByType(100);
		while(information==null||inId==maxId||information.getInType()>200){
			inId++;
			information=informationService.selectByPrimaryKey(inId);
		}
		HttpSession session=req.getSession();
		Integer usersUId = (Integer)session.getAttribute("uId");
		PaginationVo<Messages> allMessages=messagesService.getPaginationInfoByInid(inId+1,1,5);
		map.put("nowpage",1);
		int allpage=messagesService.messagePageNum(inId+1);
		map.put("allpage", allpage);
		map.put("messages",allMessages);
		map.put("uId", usersUId);
		map.put("one",information);
		map.put("maxId",maxId);
		map.put("minId", minId);
		return local+"wenzhang";
	}
	@RequestMapping("/next.html")
	public String nextOneInfo(Integer inId,ModelMap map,HttpServletRequest req){
		Information information=informationService.selectByPrimaryKey(inId-1);
		Integer maxId=informationService.selectMaxIdByType(100);
		Integer minId=informationService.selectMinIdByType(100);
		while(information==null||inId==minId||information.getInType()>200){
			inId--;
			information=informationService.selectByPrimaryKey(inId);
		}
		HttpSession session=req.getSession();
		Integer usersUId = (Integer)session.getAttribute("uId");
		PaginationVo<Messages> allMessages=messagesService.getPaginationInfoByInid(inId-1,1,5);
		map.put("nowpage",1);
		int allpage=messagesService.messagePageNum(inId-1);
		map.put("allpage", allpage);
		map.put("messages",allMessages);
		map.put("uId", usersUId);
		map.put("one",information);
		map.put("maxId",maxId);
		map.put("minId", minId);
		return local+"wenzhang";
	}
	@RequestMapping(value="moreinfo",method=RequestMethod.GET)
	public String list(@RequestParam(value = "p", defaultValue = "1")Integer pagenum,Integer offset,ModelMap map){
		if(offset==null){
			offset=1;
		}
		PaginationVo<Information> allart=informationService.getPaginationInfo(pagenum,10);
		map.put("all",allart);
		map.put("type", "文章列表");
		return local+"moreinfo";
	}
	@ResponseBody
	@RequestMapping(value="addmessage.json",method=RequestMethod.POST)
	public int addMessage(Messages messages){
		return messagesService.insert(messages);
	}
	
	public PaginationVo<Messages> getMessages(Integer inId){
		PaginationVo<Messages> allMessages=messagesService.getPaginationInfoByInid(inId,0,5);
		return allMessages;
	}
	@ResponseBody
	@RequestMapping(value="messagepage.json",method=RequestMethod.GET)
	public PaginationVo<Messages> messagepage(@RequestParam(value="pagenum", defaultValue = "1")int pagenum,int inId,ModelMap map){
		PaginationVo<Messages> pageVo= new PaginationVo<Messages>();
		pageVo=messagesService.getPaginationInfoByInid(inId,pagenum,5);
		int allpage=messagesService.messagePageNum(inId);
		map.put("nowpage",pagenum);
		map.put("allpage", allpage);
		return pageVo;
	}
}
